package com.ohos.carbon.slice.demos;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class ShareToolbarSlice extends AbilitySlice {

    private Image back_button, fei, xing, ren, xin;
    private Text text_title, xian;
    private Component share, share_layout;
    private AnimatorProperty d_share;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_sharetoolbar);
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Share toolbar")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Share toolbar")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Share toolbar", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Share toolbar", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Share toolbar", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Share Toolbar");

        share = findComponentById(ResourceTable.Id_share);
        share_layout = findComponentById(ResourceTable.Id_share_layout);
        fei = (Image) findComponentById(ResourceTable.Id_fei);
        xing = (Image) findComponentById(ResourceTable.Id_xing);
        ren = (Image) findComponentById(ResourceTable.Id_ren);
        xin = (Image) findComponentById(ResourceTable.Id_xin);
        xian = (Text) findComponentById(ResourceTable.Id_xian);
        back_button.setClickedListener(component -> onBackPressed());
        share.setClickedListener(component -> {
            share.setVisibility(Component.HIDE);
            share_layout.setVisibility(Component.VISIBLE);


//            d_share = share.createAnimatorProperty();

            AnimatorProperty d_fei = fei.createAnimatorProperty();
            AnimatorProperty d_xing = xing.createAnimatorProperty();
            AnimatorProperty d_ren = ren.createAnimatorProperty();
            AnimatorProperty d_xin = xin.createAnimatorProperty();

//            d_share.alpha(0).setDuration(30).start();

            d_fei.moveFromX(800).moveToX(55).alpha(1).setDuration(20).start();
            d_xing.moveFromX(800).moveToX(210).alpha(1).setDuration(40).start();
            d_ren.moveFromX(800).moveToX(355).alpha(1).setDuration(50).start();
            d_xin.moveFromX(800).moveToX(500).alpha(1).setDuration(60).start();
        });

//        Component componentById = findComponentById(ResourceTable.Id_Refresh);
        //setComponentPosition
//        componentById.setComponentPosition();

    }

    @Override
    protected void onBackground() {
        super.onBackground();
        share.setVisibility(Component.VISIBLE);
        share_layout.setVisibility(Component.HIDE);
//        d_share.alpha(1).setDuration(30).start();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
